// gamecameramanipulator.cpp

// 2008-05-02 11:21:51 (Jake): created

#include "crankit_precompiled.h"
#include "game.h"
#include "gamecameramanipulator.h"

GameCameraManipulator::GameCameraManipulator(Game* game) : _game(game)
{
	
}

void GameCameraManipulator::setByMatrix(const osg::Matrixd& matrix)
{
	_matrix = matrix;
}

osg::Matrixd GameCameraManipulator::getMatrix() const
{
	return _matrix;
}

osg::Matrixd GameCameraManipulator::getInverseMatrix() const
{
//	return osg::Matrixd::inverse(_matrix);
    return osg::Matrixd::translate(-_eye)*osg::Matrixd::rotate(_rotation.inverse());
}

void GameCameraManipulator::computeHomePosition() 
{ 

}

void GameCameraManipulator::home(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us) 
{ 

}

void GameCameraManipulator::init(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us) 
{ 

}

bool GameCameraManipulator::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us) 
{ 
	if (ea.getEventType() == osgGA::GUIEventAdapter::EventType::FRAME) {
//		double t = osg::Timer::instance()->time_s();
//		_game->setTime(nv->getFrameStamp()->getSimulationTime());
//		_game->setTime(t);		

		Song* currentSong = _game->getSong();
		if (currentSong != 0) {
//			float bpm = currentSong->getBaseBPM();
//			float velocity = 0.001*60*bpm;
//			float time = _game->getTime();
//			float pos = (time-3) * velocity;

			float pos = -3;

			osg::Vec3 center(0, pos+3, 0);
			osg::Vec3 eye(0, pos, 0.8);
			osg::Vec3 up(0, 0, 1);
			_matrix = osg::Matrixd::lookAt(eye, center, up);

			_eye = eye;
			_rotation.makeRotate(-30, osg::Vec3(1, 0, 0));
		}
	}

	return false;
}

void GameCameraManipulator::getUsage(osg::ApplicationUsage& usage) const 
{ 

}
